<?php
/**
 * Created by JetBrains PhpStorm.
 * User: patch
 * Date: 07.12.12
 * Time: 2:16
 * To change this template use File | Settings | File Templates.
 */

/**
 * @return array
 */
function aia_conversations_list() {
  global $user;

  $header = array(
    'new' => array(),
    'subject' => array('data' => t('Subject')),
    'user' => array('data' => t('User')),
    'last_message' => array('data' => t('Last message'))
  );
  $conversations = array();
  $cids = db_select('aia_messages', 'm')
    ->distinct()
    ->fields('m', array('cid', 'status'))
    ->condition(
    db_or()
      ->condition('ufrom', $user->uid)
      ->condition('uto', $user->uid))
    ->execute()->fetchAllAssoc('cid');

  if(sizeof($cids) > 0) {
    $conversations = db_select('aia_conversation', 'c')
      ->extend('pagerDefault')
      ->extend('tableSort')
      ->fields('c')
      ->condition('cid', array_keys($cids), 'in')
      ->limit(50)
      ->orderByHeader($header)
      ->execute()->fetchAll();
  }

  $rows = array();

  foreach($conversations as $key => $conversation) {

    $interlocutor = user_load($conversation->last_user);
    $has_new = $conversation->last_user !==  $user->uid ? '' : 'hide';

    $rows[$conversation->cid] = array(
      'new' => '<i class="conversation_' . $conversation->cid . ' icon-envelope ' . $has_new . '"></i>',
      'subject' => l($conversation->subject, 'account/conversations/' . $conversation->cid, array()),
      'user' => $interlocutor->name,
      'last_message' => format_date($conversation->updated),
    );
  }

  return array(
    'table' => array(
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $rows,
      '#empty' => t('None'),
    ),
    'pager' => array('#theme' => 'pager'),
  );
}

/**
 * @param $conversation
 * @return string
 */
function aia_conversation_page($conversation) {
  global $user;
  $messages = aia_messages_by_conversation_load($conversation->cid);
//  $messages = aia_messages_load_multiple($conversation->cid);

  $interlocutors = array();
  foreach(get_interlocutors_by_cid($conversation->cid) as $interlocutor) {
    $interlocutors[$interlocutor->uid]['avatar'] = isset($interlocutor->picture->uri) ? image_style_url('user_thumbnail', $interlocutor->picture->uri) : '';
    $interlocutors[$interlocutor->uid]['name'] = $interlocutor->name;
  }

  $row = 0;
  $content = '';

  foreach($messages as &$message) {
    $message->created = format_date($message->created, 'short');
    $content .= theme('aia_conversation_message', array('message' => $message, 'interlocutors' => $interlocutors));
    $row++;
  }

  drupal_add_js(drupal_get_path('module', 'aia_messages') . '/aia_messages.js');
  drupal_add_js(array(
//      'aia_messages' => $messages,
      'aia_conversation' => (array) $conversation,
      'max_message_id' => max(array_keys($messages)),
      'template' => theme('aia_conversation_message', array('message' => null, 'interlocutors' => null)),
      'interlocutors' => $interlocutors,
    ),
    'setting');

  unset($interlocutors[$user->uid]);
  $to = key($interlocutors);

  $form = drupal_get_form('aia_conversation_ajax_form', $to, $conversation->nid);

  aia_message_update_status($conversation->cid);

  return theme('aia_conversation_wrapper', array('messages' => $content, 'form' => $form));//$out;
}



function aia_messages_install_demo_data() {
  $users = user_load_multiple(false);

  unset($users[1]);

  foreach($users as $user) {
    $conversation = array(
      'nid' => 0,
      'subject' => 'sunject ' . $user->name,
      'ufrom' => $user->uid,
      'uto' => 1,
      'updated' => REQUEST_TIME,
      'last_user' => 1,
    );

    $cid = db_insert('aia_conversation')
      ->fields($conversation)
      ->execute();

    for($j = 0; $j < 10; $j++) {
      if($j %2 == 0){
        $from = 1;
        $to = $user->uid;
      }
      else {
        $from = $user->uid;
        $to = 1;
      }
      $message = array(
        'cid' => $cid,
        'ufrom' => $from,
        'uto' => $to,
        'body' => 'body-' . $j,
        'format' => 'plain_text',
        'created' => REQUEST_TIME,
        'status' => 1,
      );

      db_insert('aia_messages')
        ->fields($message)
        ->execute();
    }

  }

  return 'install demo data';
}
